import 'package:aitao_flutter/mine/widget/hello_text_input_window.dart';
import 'package:aitao_flutter/utils/permission_util.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

import '../../const/common_const.dart';
import '../../model/hello_img_model.dart';
import '../../model/hello_text_model.dart';
import '../../request/api.dart';
import '../../request/requestManager.dart';

class SayHelloController extends GetxController {
  final List<String> tabList = [
    MineStringConst.charactersText,
    MineStringConst.imageText,
    MineStringConst.audioText,
  ];
  int tabIndex = 0;
  List<HelloTextModel> textList = [];
  List<HelloImageModel> imageList = [];
  @override
  void onReady() {
    super.onReady();
    getTextListData();
    getImageListData();
  }

  void deleteHelloText(HelloTextModel model) async {
    var result = await RequestManager()
        .post(API.deleteSayHelloText, params: {'msg_id': model.msgId});
    if (result != null && result['code'] == 0) {
      EasyLoading.showToast('删除成功');
      getTextListData();
    }
  }

  void addHelloText(String helloText) async {
    var result = await RequestManager()
        .post(API.addSayHelloText, params: {'msg': helloText});
    if (result != null && result['code'] == 0) {
      EasyLoading.showToast('添加成功,请等待审核');
      getTextListData();
    }
  }

  Future<void> addClick() async {
    if (tabIndex == 0) {
      showDialog(
          context: Get.context!,
          builder: (builder) {
            return HelloTextInputWindow(
              inputCallBack: (value) {
                String inputValue = value;
                if (inputValue.isEmpty) {
                  EasyLoading.showToast('请输入打招呼语');
                  return;
                }
                addHelloText(inputValue);
              },
            );
          });
    } else if (tabIndex == 1) {
      //添加图片
      debugPrint('添加图片');
      var permission = await PermissionUtil.checkPermissionPhotos(Get.context!);
      debugPrint('permission:$permission');
    } else if (tabIndex == 2) {
      //添加语音
      debugPrint('添加语音');
    }
  }

  void getTextListData() async {
    var result = await RequestManager().post(
      API.getSayHelloText,
    );
    if (result != null && result['code'] == 0) {
      List tempList = result['data']['list'];
      textList = tempList.map((e) {
        return HelloTextModel.fromJson(e);
      }).toList();
      update(['helloText']);
    }
  }

  void getImageListData() async {
    var result = await RequestManager().post(
      API.getSayHelloImg,
    );
    if (result != null && result['code'] == 0) {
      List tempList = result['data']['list'];
      imageList = tempList.map((e) {
        return HelloImageModel.fromJson(e);
      }).toList();
      update(['helloImage']);
    }
  }
}
